/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife updater.

    Ages of Strife updater is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife updater is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife updater.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AgesOfStrifeServer /*extends Frame implements WindowListener,
		ActionListener*/ {
	public static Log log = LogFactory.getLog( AgesOfStrifeServer.class );

	private static final long serialVersionUID = 3833182527841187632L;
	//private Overview overview;
	private Database database;
	private GenerateDB generateDB;
	private String dbAddress = "192.168.0.1";
	private Properties properties;

	/*public void windowOpened( WindowEvent arg0 ) {
	}

	public void windowClosing( WindowEvent arg0 ) {
		System.exit( 0 );
	}

	public void windowClosed( WindowEvent arg0 ) {
	}

	public void windowIconified( WindowEvent arg0 ) {
	}

	public void windowDeiconified( WindowEvent arg0 ) {
	}

	public void windowActivated( WindowEvent arg0 ) {
	}

	public void windowDeactivated( WindowEvent arg0 ) {
	}

	public void actionPerformed( ActionEvent arg0 ) {
	}*/

	public String getDBAddress() {
		return dbAddress;
	}

	public static void main( String[] args ) {
		boolean resetGame = false;
		boolean start = false;
		if( args != null && args.length > 0 ) {
			if( "--reset".equals( args[0] ) ) {
				resetGame = true;
				start = true;
			}
			else if( "--help".equals( args[0] ) ) {
				System.out.println( "Commands:" );
				System.out.println( "Run AgesOfStrifeServer with any of the following commands:" );
				System.out.println( "Option: --start to start the turn updater" );
				System.out.println( "Option: --reset to reset the game database" );
				System.out.println( "Option: --help to display this information" );
				start = false;
			}
			else if( "--start".equals( args[0] ) ) {
				start = true;
			}
			
			if( start ) {
				new AgesOfStrifeServer( resetGame );
			}
		}
		else {
			Properties tmpProperties = new Properties();
			try {
				tmpProperties.load( new FileInputStream( "agesofstrifeserver.properties" ) );
				
			} catch ( FileNotFoundException fileNotFoundException ) {
				log.error( "Error while reading properties file", fileNotFoundException );
			} catch ( Exception e ) {
				log.error( "Unknown error", e );
			}
			
			String cmd = tmpProperties.getProperty( "startcommand" ); 
			try	{
				Runtime.getRuntime().exec( cmd );
			}
			catch ( IOException exception ) {
				System.out.println( "Error running AgesOfStrifeServer. (" + cmd + ")" );
				exception.printStackTrace();
			}
		}
	}

	public AgesOfStrifeServer( boolean resetGame ) {
		try {
			log.info( "AgesOfStrife - Turn updater" );
			properties = new Properties();
			properties.load( new FileInputStream( "agesofstrifeserver.properties" ) );
			log.debug( "Properties file loaded" );
	
			/*setTitle( "Ages of Strife - Turn updater" );
			setLayout( new BorderLayout() );
			setFont( new Font( "SansSerif", Font.BOLD, 12 ) );
			setBackground( Color.lightGray );
			setBounds( 0, 0, 300, 240 );
			addWindowListener( this );
			overview = new Overview( this );
			add( overview, "Center" );
			setVisible( true );*/
			database = new Database( this );
			generateDB = new GenerateDB( this );
			log.info( "Initialised" );
			if( !resetGame ) {
				start();
				log.info( "Started" );
			}
			else {
				log.info( "Starting game reset..." );
				System.out.println( "Starting game reset..." );
				resetGame();
				log.info( "Game reset complete" );
				System.out.println( "Game reset complete" );
			}
		} catch ( FileNotFoundException fileNotFoundException ) {
			log.error( "Error while reading properties file", fileNotFoundException );
		} catch ( Exception e ) {
			log.error( "Error setting up Ages of Strife - Updater", e );
		}
	}

	/*public void setTurn( int turn ) {
		overview.setTurn( turn );
	}

	public void setMaxTurn( int maxTurn ) {
		overview.setMaxTurn( maxTurn );
	}

	public void setSpeed( int speed ) {
		overview.setSpeed( speed );
	}

	public void setUserCount( int userCount ) {
		overview.setUserCount( userCount );
	}

	public void setRulerCount( int rulerCount ) {
		overview.setRulerCount( rulerCount );
	}

	public void setCountDown( int countDown ) {
		overview.setCountDown( countDown );
	}

	public void setStatus( String status ) {
		overview.setStatus( status );
	}*/

	public void start() {
		database.startTurns();
	}

	/*public void stop() {
		database.stopTurns();
	}*/

	public void resetGame() {
		generateDB.clearDatabase();
		//generateDB.generateNewGame();
	}
	
	public String getDatabaseLocation() {
		return properties.getProperty( "database_location" );
	}

	public String getDatabasePort() {
		return properties.getProperty( "database_port" );
	}

	public String getDatabaseUser() {
		return properties.getProperty( "database_user" );
	}

	public String getDatabasePassword() {
		return properties.getProperty( "database_password" );
	}

	public String getDatabaseName() {
		return properties.getProperty( "database_name" );
	}

	public String getMailHost() {
		return properties.getProperty( "smtp" );
	}

	public String getMailPort() {
		return properties.getProperty( "smtp_port" );
	}
	
	public String getMailSender() {
		return properties.getProperty( "mail_sender" );
	}
	
	public boolean getResetGameEnabled() {
		return "true".equalsIgnoreCase( properties.getProperty( "reset_game_enabled" ) );
	}
}
